package com.example.xiaolu.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MyDigest {
    public static String digest(String userPassword){
        try {
            // 创建MD5实例
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 更新数据到MessageDigest
            md.update(userPassword.getBytes());

            // 完成哈希计算并返回结果
            byte[] digest = md.digest();

            // 将结果转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5算法不存在", e);
        }
    }
}
